/*
 * @Author: your name
 * @Date: 2020-05-06 11:11:18
 * @LastEditTime: 2020-08-05 16:46:28
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \truck\platform\sqlite\xeSqlite.h
 */
#ifndef _XE_SQLITE3_H
#define _XE_SQLITE3_H

#include "xeLOG_internal.h"
#include "global_common.h"
#include "sqlite3.h"

extern sqlite3* g_userMgntDb;
#define  USERMGNT_DB     "./userMgnt.db" 


#define  SYSMGNT_DB  "./sysMgnt.db"
#define  IPCMGNT_VER "version"
#define  IPCMGNT_IPCMGNT_TBL  "ipcMgnt"
#define  HARD_DISK_SPACE_MGNT_TBL    "space_mgnt_tbl"


#define  URLMAP_DB   "./urlMap.db"
#define  MEMORY_DB   ":memory:" //for url map
#define  URLMAP_TBL  "url2func" 

#define IPC_DB_FILENAME "ipc.db"
#define IPC_FILE_TBL    "ipc_file_tbl"
#define IPC_STATIS_TBL    "ipc_statistics_tbl"

#define  REGION_DB  "./region.db"
#define  REGION_TREE_TBL "region_tree_tbl"

#define MYSQLITE3_OK (0)
#define MYSQLITE3_OPEN_FAIL (-1)
#define MYSQLITE3_CREATE_TABLE_FAIL (-2)
#define MYSQLITE3_DELETE_TABLE_FAIL (-3)
#define MYSQLITE3_INSERT_FAIL (-4)
#define MYSQLITE3_DELETE_FAIL (-5)
#define MYSQLITE3_UPDATE_FAIL (-6)
#define MYSQLITE3_SELECT_FAIL (-7)
#define MYSQLITE3_TRANS_BEGIN_FAIL (-8)
#define MYSQLITE3_TRANS_COMMIT_FAIL (-9)
#define MYSQLITE3_TRANS_END_FAIL (-10)
#define MYSQLITE3_TRANS_ROLLBACK_FAIL (-11)

typedef enum _query_file_type{
    E_QUERY_OLDEST = 0,
    E_QUERY_NEWEST = 1,
    E_QUERY_BY_NAME = 2,
    E_QUERY_BY_TIME = 3,

    E_QUERY_IPCLIST_ALL =4,
    E_QUERY_IPCLIST_BY_GTSSN =5,
}e_query_type;

typedef enum _insert_file_type{
    E_OPT_INVALID = -1,
    E_UPDATE_RECORD = 0,
    E_INSERT_RECORD = 1,
    E_UPDATE_STATIS = 2,
    E_DELETE_RECORD = 3,
    E_QUERY_RECORD  = 4,
}e_opt_type;

typedef enum _e_tbl_type{
    //sysmngt.db
    E_TBL_IPCMGNT = 0,
    E_TBL_SPACE = 1,

    //ipc.db
    E_TBL_FILE = 2,
    E_TBL_STATIS = 3,
}e_tbl_type;

int  db_openDB();
void db_closeDb(sqlite3 * db);

int  db_transBegin(sqlite3 *db);
int  db_transCommit(sqlite3 *db);
int  db_transEnd(sqlite3 *db);
int  db_transRollback(sqlite3 *db);

int  db_fillUrlMapDB();
void db_selectUrlMap(MyHttpPack myhttppack);
int  db_generalExec(sqlite3 *db,char * cmd);

void sysMgnt_exit();
int  db_insertInterfae(const char * h_url,http_handle m_get,http_handle m_post,http_handle m_delete,http_handle m_query);

int  db_createGeneralTbl(sqlite3 *db,const char *cmd,char *tblName);


int tiny_rec_sqlite_main();

int tiny_rec_query_sysmgntdb_info(e_tbl_type tbl_type,void *data);
int tiny_rec_query_sysmgntdb_ipclist(e_query_type query_type,char *collector_sn,void *data);
int tiny_rec_update_sysmgntdb_tbl(e_tbl_type tbl_type,e_opt_type opt_type,void *data);

int tiny_ipc_db_init(const char *path);
int tiny_get_statistics_ipcdb(const char *path,void *data);
int get_one_record_from_ipcdb(e_query_type query_type,const char *path,t_ipc_file_tbl_des *file_des);
int get_reccords_from_ipctable(e_query_type query_type,
    t_ipc_query_info *ipc_info,t_ipc_query_result *query_result);
int tiny_update_ipcdb(e_opt_type inset_type,const char *path,void *data);

int db_FileDbSwitchMemDb(sqlite3 *memDb, char *fileDbPathName, int F2M0_M2F1);

int tiny_region_init();
#endif //_XE_SQLITE3_H


